package com.qiu.wxapi.controller;


import com.qiu.wxapi.commom.ResponseResult;
import com.qiu.wxapi.mapper.HolidayMapper;
import com.qiu.wxapi.pojo.Holiday;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("holiday")
public class HolidayController {
    @Autowired
    private HolidayMapper holidayMapper;

    @PostMapping
    public ResponseResult submit(@RequestBody Holiday holiday){
        if (holiday==null){
            return ResponseResult.fail("假条不能为空",null);
        }
        holiday.setCreated(new Date());
        holiday.setState(false);
        int insert = this.holidayMapper.insert(holiday);
        System.out.println(holiday);
        return ResponseResult.success(holiday);
    }

    @GetMapping
    public ResponseResult userFind(@RequestParam("userId") Integer userId){
        Example example = new Example(Holiday.class);
        example.setOrderByClause("created  DESC");
        example.createCriteria().andEqualTo("userId",userId);
        List<Holiday> select = this.holidayMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(select)){
            return ResponseResult.fail(null);
        }
        return ResponseResult.success(select);
    }
    @GetMapping("administratorFind")
    public ResponseResult administratorFind(){
        Example example = new Example(Holiday.class);
        example.setOrderByClause("created  DESC");
        example.createCriteria().andEqualTo("state",false)
                .andEqualTo("approveId",null)
                .andEqualTo("result",null);
        List<Holiday> select = this.holidayMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(select)){
            return ResponseResult.fail(null);
        }
        return ResponseResult.success(select);
    }

    @PostMapping("approve")
    public ResponseResult approve(@RequestBody Holiday holiday){
        System.out.println(holiday);
        if (holiday==null){
            return ResponseResult.fail("假条不能为空",null);
        }
        int i = this.holidayMapper.updateByPrimaryKey(holiday);

        return ResponseResult.success("操作成功",i);
    }


}
